package hw;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Chuliqi {

    public static void main(String[] args) {
        List<List<String>> lists = matchComputer("0 1 4 5 6 7".split(" "), 1);
        System.out.println(lists);
    }

    private static List<List<String>> matchComputer(String[] processors, int num) {
        List<String> partA = new ArrayList<>();
        List<String> partB = new ArrayList<>();
        for (String processor : processors) {
            if ("0123".contains(processor)) {
                partA.add(processor);
            } else {
                partB.add(processor);
            }
        }
        List<List<String>> result = new ArrayList<>();
        switch (num) {
            case 1:
                // 当为1时
                if (partA.size() == 1 || partB.size() == 1) {
                    if (partA.size() == 1) dfs(partA, 0, 1, result, new ArrayList<String>());
                    if (partB.size() == 1) dfs(partB, 0, 1, result, new ArrayList<String>());
                    return result;
                }
                if (partA.size() == 3 || partB.size() == 3) {
                    if (partA.size() == 3) dfs(partA, 0, 1, result, new ArrayList<String>());
                    if (partB.size() == 3) dfs(partB, 0, 1, result, new ArrayList<String>());
                    return result;
                }
                if (partA.size() == 2 || partB.size() == 2) {
                    if (partA.size() == 2) dfs(partA, 0, 1, result, new ArrayList<String>());
                    if (partB.size() == 2) dfs(partB, 0, 1, result, new ArrayList<String>());
                    return result;
                }
                if (partA.size() == 4 || partB.size() == 4) {
                    if (partA.size() == 4) dfs(partA, 0, 1, result, new ArrayList<String>());
                    if (partB.size() == 4) dfs(partB, 0, 1, result, new ArrayList<String>());
                    return result;
                }
            case 2:
                if (partA.size() == 2 || partB.size() == 2) {
                    if (partA.size() == 2) dfs(partA, 0, 2, result, new ArrayList<String>());
                    if (partB.size() == 2) dfs(partB, 0, 2, result, new ArrayList<String>());
                    return result;
                }
                if (partA.size() == 4 || partB.size() == 4) {
                    if (partA.size() == 4) dfs(partA, 0, 2, result, new ArrayList<String>());
                    if (partB.size() == 4) dfs(partB, 0, 2, result, new ArrayList<String>());
                    return result;
                }
                if (partA.size() == 3 || partB.size() == 3) {
                    if (partA.size() == 3) dfs(partA, 0, 2, result, new ArrayList<String>());
                    if (partB.size() == 3) dfs(partB, 0, 2, result, new ArrayList<String>());
                    return result;
                }
            case 4:
                if (partA.size() == 4 || partB.size() == 4) {
                    if (partA.size() == 4) dfs(partA, 0, 4, result, new ArrayList<String>());
                    if (partB.size() == 4) dfs(partB, 0, 4, result, new ArrayList<String>());
                    return result;
                }
            case 8:
                if (partA.size() == 4 && partB.size() == 4) {
                    result.add(Arrays.asList(processors));
                    return result;
                }
        }
        return result;
    }

    private static void dfs(List<String> partA, int i, int i1, List<List<String>> result, ArrayList<String> path) {
        if (path.size() == i1) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int j = i; j < partA.size(); j++) {
            path.add(partA.get(j));
            dfs(partA, j + 1, i1, result, path);
            path.remove(path.size() - 1);
        }
    }

}
